<template>
	<view v-if="list.length > 0">
		<!-- 模态框 -->
		<u-modal 
			:show="show"
			:title="title"
			:content='content'
			:closeOnClickOverlay="true"
			@close="show=false"
			@confirm="handlerComfirm"
			@cancel="show=false"
			:showCancelButton="true"
		></u-modal>
		<view class="" v-for="(item, index) in list" :key="index">
			<u-cell :isLink="true" :border="true" @click="navigateTo(item.warehouseArea)">
				<view slot="title" class="notice-record-title" style="align-items: center;display: flex;">
					<text>移库单[{{item.taskId}}]</text>
					<view style="margin-left: 50rpx;">
						<u-button type="primary" size="small" plain :disabled="item.status === 1" @click.native.stop="confirm(item.taskId)">完成</u-button>
					</view>
				</view>
				<view slot="label" class="notice-record-desc">
					<u-line length="106%"></u-line>
					<view style="display: flex;justify-content: space-between;margin: 10rpx 0;">
						<view>{{ item.createTime }}</view>
						<view>{{ getAreaType(item.warehouseArea) }}</view>
						<view>{{ getOrderStatus(item.status) }}</view>
					</view>
				</view>
			</u-cell>
			<u-gap height="10" bgColor="#ebebeb"></u-gap>
		</view>
		<u-loadmore :status="status" />
		<u-toast ref="uToast"></u-toast>
	</view>
	<view v-else>
		<u-empty
			mode="list"
			icon="http://cdn.uviewui.com/uview/empty/list.png"
		>
		</u-empty>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import { moveOrder, comfirmOrder } from '@/api/work/move.js'
	export default {
		data() {
			return {
				query: {
					pageNo: 1,
					pageSize: 10
				},
				total: 0,
				loadFinish:false,
				status: 'loadmore',
				list: [],
				taskId: '',
				show:false,
				title:'操作提示',
				content:'点击此按钮,该移库单将完成,是否继续?'
			};
		},
		onLoad() {
			this.toQuery()
		},
		methods: {
			// 搜索
			async toQuery() {
				const res = await moveOrder({})
				this.list = res.records.filter(v => v.status === 0)
				this.total = res.total
				if(this.total < this.query.pageSize) {
					this.loadFinish = true
					this.status = 'nomore'
				}
			},
			// 移库单类型
			getAreaType(val) {
				switch(val) {
					case 'L':
						return '料箱库'
					case 'P':
						return '托盘库'
				}
			},
			// 移库单状态
			getOrderStatus(val) {
				switch(val) {
					case 0:
						return '未完成'
					case 1:
						return '已完成'
				}
			},
			navigateTo(val) {
				switch (val) {
					case 0:
						uni.navigateTo({
							url: ''
						})
						break
					case 1:
						uni.navigateTo({
							url: ''
						})
						break
					case 'AT1':
						uni.navigateTo({
							url: '/pages/work/move/moveBin'
						})
						break
				}
			},
			// 完成按钮
			confirm(taskId) {
				this.taskId = taskId
				this.show = true
			},
			async handlerComfirm() {
				const res = await comfirmOrder({taskId: this.taskId})
				this.$refs.uToast.show({
					type: 'success',
					message: '操作成功,该任务已完成'
				})
				this.query = {
					pageNo: 1,
					pageSize: 10
				}
				this.taskId = ''
				this.show = true
				this.toQuery()
			},
			// 触底
			onReachBottom() {
				this.status = 'loading';
				if(this.loadFinish) {
					this.status = 'nomore'
					this.$refs.uToast.show({
						type: 'warning',
						message: "移库单已加载完毕"
					})
					return
				} else {
					this.query.pageNo++
				}
				moveOrder(this.query).then(res => {
					this.list.push(...res.records.filter(v => v.status === 0))
					this.total = res.total
					this.status = 'loadmore'
					if( this.query.pageNo * this.query.pageSize > res.total ) {
						this.loadFinish = true
						this.status = 'nomore'
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
/deep/.u-cell__body {
	padding: 12rpx !important;
	align-items: start;
}
	
.notice-record-title {
	margin: 10px 0;
}
.notice-record {
  display: flex;
  padding: 0;

  &-content {
    margin-left: 8px;
  }

  &-title {
    font-size: 28rpx;
    font-weight: bold;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  &-title .read {
    color: #ccc;
  }

  &-desc {
    padding: 4px 0;
    font-size: 12px;
    color: #909399;
    // display: flex;
  }
}
</style>
